/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 * Description: 性能评估器相关头文件
 * Author: qingjicheng
 * Create: 2022-8-1
 */

#ifndef EVALUATOR_LOG_DEF_H
#define EVALUATOR_LOG_DEF_H
#include <chrono>
#include "log.h"

namespace HcclEvaluator {
using TimePoint = std::chrono::steady_clock::time_point;
inline TimePoint TimeNow()
{
    return std::chrono::steady_clock::now();
}

template<typename TimeType>
inline uint64_t DurationUs(const TimeType &point)
{
    return std::chrono::duration_cast<std::chrono::microseconds>(point).count();
}
} // namespace HcclEvaluator

#define EVALUATOR_ERROR(...) HCCL_ERROR(__VA_ARGS__)

#define EVALUATOR_WARNING(...) HCCL_WARNING(__VA_ARGS__)

#define EVALUATOR_INFO(...) HCCL_INFO(__VA_ARGS__)

/* 检查函数返回值, 并返回指定错误码 */
#define EVALUATOR_CHK_RET(call)                                  \
    do {                                                         \
        HcclResult ret = call;                                   \
        if (ret != HCCL_SUCCESS) {                               \
            if (ret == HCCL_E_AGAIN) {                           \
                EVALUATOR_WARNING("call trace: ret -> %d\n", ret); \
            } else {                                             \
                EVALUATOR_ERROR("call trace: ret -> %d\n", ret);   \
            }                                                    \
            return ret;                                          \
        }                                                        \
    } while (0)

/* 检查指针, 若指针为NULL, 则记录日志, 并返回错误 */
#define EVALUATOR_CHK_PTR_NULL(ptr)                                                        \
    do {                                                                                   \
        if ((ptr) == nullptr) {                                                               \
            EVALUATOR_ERROR("ptr [%s] is nullptr, return HCCL_E_PTR", #ptr);                  \
            return HCCL_E_PTR;                                                             \
        }                                                                                  \
    } while (0)
#endif